import apiXueBeng from '@/api/modules/xuebeng'

export const useXueBengStore = defineStore(
  // 唯一ID
  'xuBeng',
  () => {

    const account = ref(localStorage.xbAccount ?? '')
    const token = ref(localStorage.xbtoken ?? '')
    const isLogin = computed(() => {
      if (token.value) {
        return true
      }
      return false
    })

    // 登录
    async function login(data: {
      type: number,
      phone: string
      password: string
    }) {
      const res = await apiXueBeng.login(data)
      console.log(res)
      localStorage.setItem('xbAccount', res.data.data.phone)
      localStorage.setItem('xbtoken', res.data.data.token.token)
      account.value = res.data.data.phone
      token.value = res.data.data.token.token
    }

    async function queryNovelList(data: object) {
      return await apiXueBeng.queryNovelList(data)
    }

    async function queryNovelListFilterData() {
      return await apiXueBeng.queryNovelListFilterData()
    }

    async function getArticleById(id: number) {
      return await apiXueBeng.getArticleById(id)
    }

    async function addKeywords(data: object) {
      return await apiXueBeng.addKeywords(data)
    }

    return {
      account,
      token,
      isLogin,
      login,
      queryNovelList,
      queryNovelListFilterData,
      getArticleById,
      addKeywords
    }
  },
)
